/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entityFacades;

import entities.MaintenanceProject;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author wesleycl
 */
@Stateless
public class MaintenanceProjectFacade implements MaintenanceProjectFacadeLocal {
    @PersistenceContext(name="spssample-ejbPU")
    private EntityManager em;

    public void create(MaintenanceProject maintenanceProject) {
        em.persist(maintenanceProject);
    }

    public void edit(MaintenanceProject maintenanceProject) {
        em.merge(maintenanceProject);
    }

    public void remove(MaintenanceProject maintenanceProject) {
        em.remove(em.merge(maintenanceProject));
    }

    public MaintenanceProject find(Object id) {
        return em.find(MaintenanceProject.class, id);
    }

    public List<MaintenanceProject> findAll() {
        return em.createQuery("select object(o) from MaintenanceProject as o").
                getResultList();
    }

     public List<MaintenanceProject> findByRange(int start_id,int end_id) {
        return em.createQuery("select object(o) from MaintenanceProject as o where " +
                "o.id between :start and :end ")
                .setParameter("start", start_id)
                .setParameter("end", end_id)
                .getResultList();
    }

      public List<MaintenanceProject> findByRangeWithFetch(int start_id,int end_id) {
        return em.createQuery("select object(o) from MaintenanceProject as o " +
                " left outer join fetch o.vehicle " +
                " left outer join fetch o.customer where " +
                "o.id between :start and :end ")
                .setParameter("start", start_id)
                .setParameter("end", end_id)
                .getResultList();
    }

}
